package com.pengtu.gsj.controller.yh;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pengtu.gsj.controller.base.DefaultBaseController;
import com.pengtu.gsj.dao.base.Page;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.entity.yh.RoadRectification;
import com.pengtu.gsj.interceptor.Token;
import com.pengtu.gsj.service.yh.RoadRectificationService;
import com.pengtu.gsj.utils.MathsUtils;
import com.pengtu.gsj.utils.web.SpringMvcUtils;

/**
 * 
 * 路段整改信息管理
 * RoadRectificationController
 * 
 * 2017年7月31日 上午11:28:12
 * @author zl
 * @version 1.0.0
 *
 */
@Controller
@RequestMapping("roadRect")
public class RoadRectificationController extends DefaultBaseController{
	@Autowired
	private RoadRectificationService roadRectificationService;
	
	@ModelAttribute
	public RoadRectification loadEntity(String id) {
		RoadRectification roadRectification = null;
		if (StringUtils.isNotEmpty(id)) {
			roadRectification = roadRectificationService.getRoadRectification(id);
		} else {
			roadRectification = new RoadRectification();
		}
		return roadRectification;
	}
	
	
	@RequestMapping("listInfo")
	@Token(save = true)
	public String showLisfInfo() {
		return "yh/roadRect_list";
	}
	
	@RequestMapping(value="list", produces = "application/json; charset=utf-8")
	@ResponseBody
	public Page<RoadRectification> getRoadRectificationList(Page<RoadRectification> page, HttpServletRequest request){
		QueryFilter queryFilter = QueryFilter.buildFromHttpRequest(request);
		if (!page.isOrderBySetted()) {
			page.setOrderBy("createDate");
			page.setOrder(Page.DESC);
		}
		page = roadRectificationService.findRoadRectifications(page, queryFilter);
		return page;
	}
	
	@RequestMapping(value = "save", method=RequestMethod.POST)
	@ResponseBody
	@Token(remove = true)
	public boolean saveRoadRectification(@ModelAttribute RoadRectification roadRectification) {
		roadRectification.setRectificationRate(MathsUtils.divDouble(roadRectification.getRectification()*100, MathsUtils.sumDouble(roadRectification.getMinisterial(), roadRectification.getProvincial()), 2));
		roadRectificationService.saveRoadRectification(roadRectification);
		return true;
	}
	
	@RequestMapping(value="show")
	@ResponseBody                                                     
	public RoadRectification showRoadRectification (@ModelAttribute RoadRectification roadRectification){
		return roadRectification;
	}
	
	@RequestMapping(value="delete")
	@ResponseBody
	public boolean deleteRoadRectification(@ModelAttribute RoadRectification roadRectification){
		roadRectificationService.deleteRoadRectification(roadRectification);
		return true;
	}
	
	/**
	 * 
	 * checkYear:
	 * 适用:新增和修改路段整改信息时，判断年份是否重复
	 * @return 返回true或false  json数据
	 * @exception 
	 * @since  1.0.0
	 */
	@ResponseBody
	@RequestMapping(value="checkYear",produces = "application/json; charset=utf-8")
	public Boolean checkYear() {
		String entityId = SpringMvcUtils.getParameter("entityId");
		String year = SpringMvcUtils.getParameter("year");
		Boolean isExist = roadRectificationService.isYearUnique(year, entityId);
		return isExist;
	}
}
